package com.hf.www.base.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hf.www.base.model.SysUserModel;
import com.hf.www.tools.SysConstants;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

public class BaseController extends Controller {

	//	private static final Logger LOG = Logger.getLogger(BaseController.class);
	protected final String DATA = "d";
	protected final String ERROR = "e";
	protected final String RESULT = "r";
	protected final String MESSAGE = "m";
	private int defaultPageSize = 40;

	/**
	 * 创建分页对象
	 * 
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public Pager createPager(int pageSize, int pageNo) {
		Pager pager = null;
		if (pageSize <= 0) {
			pageSize = defaultPageSize;
		}
		if (pageNo <= 0) {
			pageNo = 1;
		}
		pager = new Pager(pageSize, pageNo);
		return pager;
	}

	public SysUserModel getUser() {
		return (SysUserModel) getSession().getAttribute(SysConstants.SESSION_USER);
	}

	/**
	 * 创建分页对象
	 * 
	 * @param request
	 * @return
	 */
	public Pager createPager() {
		Pager pager = null;
		String pageSize = getPara("rows");
		String pageNo = getPara("page");
		if (StrKit.isBlank(pageSize)) {
			pageSize = String.valueOf(defaultPageSize);
		}
		if (StrKit.isBlank(pageNo)) {
			pageNo = "1";
		}
		pager = new Pager(Integer.valueOf(pageSize), Integer.valueOf(pageNo));
		return pager;
	}
	/**
	 * 创建查询对象
	 * 
	 * @param paramNames 需要从Request的参数集合
	 * @return
	 */
	public Pager createPager(String... paramNames) {
		Pager pager = createPager();
		String param = null;
		for (String paramName : paramNames) {
			param = getPara(paramName);
			if (!StrKit.isBlank(param)) {
				pager.addParam(paramName, param);
			}
		}

		return pager;
	}
	
	
	/**
	 * 
	 * 获取请求的指定参数值
	 * @param paramNames
	 * @return
	 */
	public Map<String, Object> getParams(String... paramNames){
		Map<String, Object> params = new HashMap<String, Object>();
		for (String paramName : paramNames) {
			String param = getPara(paramName);
			if (!StrKit.isBlank(param)) {
				params.put(paramName, param);
			}
		}
		return params ;
	}
	
	/**
	 * 获取result Map对象
	 * 
	 * @return
	 */
	public Map<String, Object> getResultMap() {
		return new HashMap<String, Object>();
	}

	/**
	 * 生成表格数据格式
	 * 
	 * @param pager
	 * @return
	 */
	public Map<String, Object> getGridData(Pager pager) {
		Map<String, Object> result = getResultMap();
		result.put("total", pager.getTotalRecord());
		result.put("rows", pager.getResult());
		return result;
	}

	/**
	 * 获取用户真实ip地址
	 * request.getRemoteAddr(); nginx做跳转不能获取
	 * 
	 * @param request
	 * @return
	 */
	public static String getRealIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
			ip = request.getHeader("Proxy-Client-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
			ip = request.getHeader("WL-Proxy-Client-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
			ip = request.getRemoteAddr();
		return ip;
	}
}
